//
//  QDJFViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "QDJFViewController.h"

#import "QDJFCell.h"
@interface QDJFViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *signArray;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) UILabel *lab2;

@property (nonatomic, strong) UILabel *lab3;

@property (nonatomic, strong) UIButton *btnQD;

@property (nonatomic, strong) NSDictionary *directionofchangeDic;

@end

@implementation QDJFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"签到积分"];
    
    self.directionofchangeDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"+",@"2", nil];
    
    self.page = 1;
    
    [self loadData];
    [self loadUI];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //内容为空不显示cell
    _tableView.tableFooterView = [UIView new];
    [self.view addSubview:_tableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page ++;
        [self loadData];
    }];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 110)];
    headView.backgroundColor = [UIColor whiteColor];
    _tableView.tableHeaderView = headView;
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矢量智能对象"]];
    [headView addSubview:imgView];
    [imgView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView.centerY);
        make.left.equalTo(headView.left).offset(30);
    }];
    
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = @"我的积分：";
    lab1.textColor = COLOR_RGB(38, 38, 38, 1);
    [headView addSubview:lab1];
    [lab1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgView.right).offset(8);
        make.top.equalTo(imgView.top).offset(4);
    }];
    
    
    self.lab2 = [[UILabel alloc] init];
    self.lab2.text = @"60";
    self.lab2.font = [UIFont boldSystemFontOfSize:16.0];
    [headView addSubview:self.lab2];
    [self.lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lab1.right).offset(0);
        make.top.equalTo(imgView.top).offset(4);
    }];
    
    self.lab3 = [[UILabel alloc] init];
    self.lab3.text = @"已签到";
    self.lab3.textColor = [UIColor darkGrayColor];
    self.lab3.font = [UIFont systemFontOfSize:14.0];
    [headView addSubview:self.lab3];
    [self.lab3 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgView.right).offset(8);
        make.bottom.equalTo(imgView.bottom).offset(-4);
    }];
    
    self.btnQD = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnQD setTitle:@"已签到" forState:UIControlStateNormal];
    [self.btnQD setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.btnQD.titleLabel.font = [UIFont systemFontOfSize:15.0];
    self.btnQD.backgroundColor = COLOR_RGB(255, 201, 107, 1);
    self.btnQD.layer.cornerRadius = 10.0;
    [self.btnQD addTarget:self action:@selector(clickQD) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.btnQD];
    [self.btnQD makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(70);
        make.height.equalTo(30);
        make.centerY.equalTo(imgView.centerY);
        make.right.equalTo(headView.right).offset(-20);
    }];
    
}
//签到功能
- (void)clickQD{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/userSign",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"签到功能res:%@",responseObject);
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        NSString *msg = [responseObject objectForKey:@"msg"];
        if ([state isEqualToString:@"200"]) {
            [SVProgressHUD showSuccessWithStatus:msg];
            [self.btnQD setUserInteractionEnabled:NO];
            [self.btnQD setTitle:@"已签到" forState:UIControlStateNormal];
            [self.lab3 setText:@"已签到"];
            NSDate *senddate = [NSDate date];
            
            NSString *date2 = [NSString stringWithFormat:@"%ld", (long)[senddate timeIntervalSince1970]];
            
            NSLog(@"date2时间戳 = %@",date2);
            NSDictionary *addDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"5",@"variablecontent",@"签到积分",@"remarks",@"2",@"directionofchange",date2,@"addtime", nil];
            [self.signArray addObject:addDic];
            [self.tableView reloadData];
        }else{
            [SVProgressHUD dismiss];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.signArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    QDJFCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QDJFCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *itemDic = [self.signArray objectAtIndex:indexPath.row];
    [cell.labDay setText:[itemDic objectForKey:@"remarks"]];
    
    [cell.labNum setText:[NSString stringWithFormat:@"%@ %@",[self.directionofchangeDic objectForKey:[itemDic objectForKey:@"directionofchange"]],[itemDic objectForKey:@"variablecontent"]]];
    [cell.labDate setText:[self getNewDate:[itemDic objectForKey:@"addtime"]]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 80, 20)];
    lab.text = @"签到记录";
    [lab setTextColor:COLOR_RGB(51, 51, 51, 1)];
    lab.font = [UIFont systemFontOfSize:15.0];
    [view addSubview:lab];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}
//签到详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/signinDetails/p/%zd",urlHead,self.page];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"签到详情res:%@",responseObject);
        NSDictionary *responDic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        NSArray *dataArray = [responseObject objectForKey:@"content"];
        if ([state isEqualToString:@"200"] && [dataArray isKindOfClass:[NSArray class]]) {
            
            if (self.page == 1) {
                 self.signArray = [dataArray mutableCopy];
            }else{
                [self.signArray addObjectsFromArray:dataArray];
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            if (dataArray.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            self.lab2.text = [responDic objectForKey:@"userScore"];
            NSString *signStringStatus = @"今天还未签到";
            NSString *signButtonString = @"签到";
            if ([[responDic objectForKey:@"sign"] integerValue] == 1) {
                signStringStatus = @"已签到";
                signButtonString = @"已签到";
                [self.btnQD setUserInteractionEnabled:NO];
            }
            [self.lab3 setText:signStringStatus];
            [self.btnQD setTitle:signButtonString forState:UIControlStateNormal];
        }
        
        [SVProgressHUD dismiss];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

- (NSMutableArray *)signArray{
    if (!_signArray) {
        _signArray = [[NSMutableArray alloc]init];
    }
    return _signArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
